<?php

class Default_Form_RegisterUser extends Zend_Form {
	const USERNAME_LABEL         = 'Username';
	const FULLNAME_LABEL         = 'Họ tên';
	const EMAIL_LABEL            = 'Email';
	const PASSWORD_LABEL         = 'Password';
	const CONFIRM_PASSWORD_LABEL = 'Xác nhận lại password';
	const TEL_LABEL              = 'Điện thoại';
	
	const ENCODING               = 'UTF-8';
	const USERNAME_MIN_LENGTH    = 6;
	const USERNAME_MAX_LENGTH    = 32;
	const FULLNAME_MIN_LENGTH    = 0;
	const FULLNAME_MAX_LENGTH    = 50;
	const PASSWORD_MIN_LENGTH    = 6;
	const PASSWORD_MAX_LENGTH    = 32;
	
	public function init() {
		
		$this->setName('registeruser');
		$id = new Zend_Form_Element_Hidden('id');
		$id->addFilter('Int');

		//User name
		$username = self::addUsername();
        //Full name
        $fullname = self::addFullName();
        //Email
        $email = self::addEmail();
		//Password
		$password = self::addPassword();
		//Confirm Password
		$cofirmpassword = self::addConfirmPassword();
		//Tel
		$tel = self::addTel();
	
        $this->addElements(array($id, $username, $password, $cofirmpassword, $email, $fullname, $tel));
	}
	
	/*
	 * Add username element
	 */
	protected  function addUsername() {
		//Alpha validate
		$usernameAlpha = new Zend_Validate_Alpha(false);
        //Length validate
        $usernameLength = new Zend_Validate_StringLength(self::USERNAME_MIN_LENGTH, self::USERNAME_MAX_LENGTH, self::ENCODING);
        //Not empty
        $usernameNotEmpty = new Zend_Validate_NotEmpty();
        $tb_name = new Admin_Model_DbTable_Users();
        $username = new Zend_Form_Element_Text('username');
        $username	->setAttrib('class','username')
        			->addFilter('StripTags')
        			->addFilter('StringTrim')
        			->setRequired(true)
        			->addValidator($usernameNotEmpty,true)
        			->addValidator($usernameLength)
        			->setDecorators(array('ViewHelper'))
        			->addValidator('regex', true, 
                       array(
                           'pattern'=>'/^[(a-zA-Z0-9\_)]+$/', 
                           'messages'=>array(
                               'regexNotMatch'=>self::USERNAME_LABEL." chứa ký tự không hợp lệ."                           )
                       ))
        			->addValidator('Db_NoRecordExists', false, array(
         						'table' => $tb_name->getTbName(),
        						'field' => 'username',
         						'messages' =>array(
         						'recordFound' => self::USERNAME_LABEL." '%value%' đã tồn tại.")));
		return $username;
	}
	
	/*
	 * Add full_name element
	 */
	protected  function addFullName() {
		$fullnameAlpha = new Zend_Validate_Alpha(true);
        //Length validate
        $fullnameLength = new Zend_Validate_StringLength(self::FULLNAME_MIN_LENGTH, self::FULLNAME_MAX_LENGTH, self::ENCODING);
        $fullname = new Zend_Form_Element_Text('ho_ten');
        $fullname	->addFilter('StripTags')
        			->addFilter('StringTrim')
        			->setDecorators(array('ViewHelper'))
        			->addValidator($fullnameLength)
        			->addValidator($fullnameAlpha);
        return $fullname;
	}

	
	
	/*
	 * Add email element
	 */
	protected  function addEmail() {
		 $tb_name = new Admin_Model_DbTable_Users();
         $email = new Zend_Form_Element_Text('email');
         $emailNotEmpty = new Zend_Validate_NotEmpty();
         $email	->setRequired(true)
         		->addValidator($emailNotEmpty, true)
         		->addValidator('Db_NoRecordExists', false, array (
         						'table' => $tb_name->getTbName(),
        						'field' => 'email',
         						'messages' =>array(
         						'recordFound' => self::EMAIL_LABEL." '%value%' đã tồn tại.")))
         		->addFilter('StripTags')
         		->addFilter('StringTrim')
         		->setDecorators(array('ViewHelper'))
         		->addValidator(new Zendhlt_Validate_Email());
         return $email;
	}
	
	/*
	 * Add password element
	 */
	protected function addPassword() {
		//Not empty validate
		$passwordNotEmpty = new Zend_Validate_NotEmpty();
        //Length validate
        $passwordLength = new Zend_Validate_StringLength(self::PASSWORD_MIN_LENGTH, self::PASSWORD_MAX_LENGTH, self::ENCODING);
		$password = new Zend_Form_Element_Password('password');
		$password	->addFilter('StripTags')
         			->addFilter('StringTrim')
         			->setDecorators(array('ViewHelper'))
         			->setRequired(true)
         			->addValidator($passwordNotEmpty,true)
         			->addValidator($passwordLength);
         return $password;
	}
	
	/*
	 * Add confirm password element
	 */
	protected function addConfirmPassword() {
		//Not empty validate
		$passwordNotEmpty = new Zend_Validate_NotEmpty();
		$passwordLength = new Zend_Validate_StringLength(self::PASSWORD_MIN_LENGTH, self::PASSWORD_MAX_LENGTH, self::ENCODING);
		$confirmpassword = new Zend_Form_Element_Password('cfrmpassword');
    	$confirmpassword->addFilter('StripTags')
						->addFilter('StringTrim')
         				->setDecorators(array('ViewHelper'))
            			->setRequired(true)
         				->addValidator($passwordNotEmpty,true)
            			->addValidator($passwordLength)
            			->addValidator('Identical', false, array('token' => 'password'));
        return $confirmpassword;
	}
	
	/*
	 * Add tel
	 */
	protected function addTel() {
		$notEmptyTelephone = new Zend_Validate_NotEmpty();
        $telephone = new Zend_Form_Element_Text('dien_thoai');
        $telValidator = new Zendhlt_Validate_Phone();
        $telephone	->setDecorators(array('ViewHelper'))
        			->addFilter('StripTags')
        			->addFilter('StringTrim');
        return $telephone;
	}
	
}
